/**************用户路由器****************/
const express = require('express')
const pool = require('../pool.js')
const router = express.Router()
module.exports = router

//1.添加一个新增用户的路由
//POST /add
//请求主体 user_name=小明&upwd=123456&email=xiaoming@qq.com&phone=18678906543&gender=1
//测试地址：http://127.0.0.1:8080/v1/user/add
router.post('/add',(req,res)=>{
	let n = req.body.user_name
	let p = req.body.upwd
	let e = req.body.email
	let phone = req.body.phone
	let g = req.body.gender
	if(!n){
		res.send({code:201,msg:'用户名不能为空！'})
		return
	}
	if(!p){
		res.send({code:202,msg:'密码不能为空！'})
		return
	}
	if(!/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/.test(e)){
		res.send({code:203,msg:'邮箱格式错误！'})
		return
	}
	if(!/^(13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\d{8}$/.test(phone)){
		res.send({code:204,msg:'电话格式错误！'})
		return
	}
	if(!g){
		res.send({code:205,msg:'性别不能为空！'})
		return
	}
	let sql = 'insert into user(user_name,upwd,email,phone,gender) values(?,?,?,?,?)'
	pool.query(sql,[n,p,e,phone,g],(err,result)=>{
		if(err) throw err
		res.send({code:200,msg:'添加成功！'})
	})
})

//2.添加一个通过用户名和密码登录的接口
//GET /login
//请求参数 user_name=dingding&upwd=123456
//测试地址：http://127.0.0.1:8080/v1/user/login?user_name=dingding&upwd=123456
router.get('/login',(req,res)=>{
	let name = req.query.user_name
	let upwd = req.query.upwd
	if(!name){
		res.send({code:201,msg:'用户名不能为空'})
		return
	}
	if(!upwd){
		res.send({code:202,msg:'密码不能为空'})
		return
	}
	let sql = 'select uid from user where user_name=? and upwd=?'
	pool.query(sql,[name,upwd],(err,result)=>{
		if(err) throw err
		if(result.length>0){
			res.send({code:200,msg:'login success'})
		}else{
			res.send({code:200,msg:'login failed'})
		}
	})
})

//3.添加一个修改用户信息的接口
//PUT /alter
//请求主体 user_name=小红&upwd=123456&email=xiaohong@qq.com&phone=18678906543&gender=0
//测试地址：http://127.0.0.1:8080/v1/user/alter
router.put('/alter',(req,res)=>{
	let n = req.body.user_name
	let p = req.body.upwd
	let e = req.body.email
	let phone = req.body.phone
	let g = req.body.gender
	if(!/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/.test(e)){
		res.send({code:203,msg:'邮箱格式错误！'})
		return
	}
	if(!/^(13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\d{8}$/.test(phone)){
		res.send({code:204,msg:'电话格式错误！'})
		return
	}
	let sql = 'update user set user_name=?,upwd=?,email=?,phone=?,gender=? where uid=?'
	pool.query(sql,[n,p,e,phone,g,req.body.uid],(err,result)=>{
		if(err) throw err
		console.log(result)
		if(result.affectedRows>0){
			res.send({code:200,msg:'修改成功'})
		}else{
			res.send({code:200,msg:'修改失败'})
		}
	})
})

//4.查询用户酒店相关信息的表格
//GET /hotel
//请求参数：user_name=dingding
//测试地址：http://127.0.0.1:8080/v1/user/hotel?user_name=dingding
router.get('/hotel',(req,res)=>{
	let uname = req.query.user_name
	let sql = 'select hotel_name,start_time,end_time from user_hotel where user_name=?'
	pool.query(sql,[uname],(err,result)=>{
		if(err) throw err
		res.send(result)
	})
})

//5.用户新增酒店相关信息的表格
//POST /hoteladd
//请求主体 user_name=jiajia&hotel_name=慕沙梦想客栈&start_time=2022-3-1&end_time=2022-3-2
//测试地址：http://127.0.0.1:8080/v1/user/hoteladd
router.post('/hoteladd',(req,res)=>{
	let uname = req.body.user_name
	let hname = req.body.hotel_name
	let stime = req.body.start_time
	let etime = req.body.end_time
	let sql = 'insert into user_hotel(user_name,hotel_name,start_time,end_time) values(?,?,?,?)'
	pool.query(sql,[uname,hname,stime,etime],(err,result)=>{
		if(err) throw err
		if(result.affectedRows>0){
			res.send({code:200,msg:'添加成功'})
		}else{
			res.send({code:201,msg:'添加失败'})
		}
	})
})

//6.查询用户名是否被占用的接口
//GET /check_uname
//请求参数 user_name=dingding
//测试地址：http://127.0.0.1:8080/v1/user/check_uname?user_name=dingding
router.get('/check_uname',(req,res)=>{
	let uname = req.query.user_name
	if(!uname){
		res.send({code:202,msg:'用户名不能为空'})
		return
	}
	if(uname.length<6 || uname.length>9){
		res.send({code:203,msg:'用户名在6位到9位之间'})
		return
	}
	let sql = 'select uid from user where user_name=?'
	pool.query(sql,uname,(err,result)=>{
		if(err) throw err
		if(result.length>0){
			res.send({code:200,msg:'exists'})
		}else{
			res.send({code:201,msg:'non-exists'})
		}
	})
})

//7.密码是否符合要求的接口
//get /check_upwd
//请求参数：upwd=123456
//测试地址：http://127.0.0.1:8080/v1/user/check_upwd?upwd=123456
router.get('/check_upwd',(req,res)=>{
	let upwd = req.query.upwd
	if(!upwd){
		res.send({code:200,msg:'密码不能为空'})
		return
	}
	if(upwd.length<6 || upwd.length>12){
		res.send({code:201,msg:'密码长度在6到12位之间'})
		return
	}
	res.send({code:202,msg:'密码格式正确'})
})

//8.检测邮箱是否符合要求的接口
//get /check_email
//请求参数：email=123@qq.com
//测试地址：http://127.0.0.1:8080/v1/user/check_email?email=123@qq.com
router.get('/check_email',(req,res)=>{
	let email = req.query.email
	if(!email){
		res.send({code:200,msg:'邮箱不能为空'})
		return
	}
	if(!/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/.test(email)){
		res.send({code:203,msg:'邮箱格式错误！'})
		return
	}
	let sql = 'select uid from user where email=?'
	pool.query(sql,email,(err,result)=>{
		if(err) throw err
		if(result.length>0){
			res.send({code:200,msg:'exists'})
		}else{
			res.send({code:201,msg:'non-exists'})
		}
	})
})
//9.检测手机号是否符合要求
//get /check_phone
//请求参数：phone=18709876543
//测试地址：http://127.0.0.1:8080/v1/user/check_phone?phone=18709876543
router.get('/check_phone',(req,res)=>{
	let phone = req.query.phone
	if(!phone){
		res.send({code:200,msg:'手机号不能为空'})
		return
	}
	if(!/^(13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\d{8}$/.test(phone)){
			res.send({code:204,msg:'电话格式错误！'})
			return
		}
	let sql = 'select uid from user where phone=?'
	pool.query(sql,phone,(err,result)=>{
		if(err) throw err
		if(result.length>0){
			res.send({code:201,msg:'exists'})
		}else{
			res.send({code:202,msg:'non-exists'})
		}
	})
})

//10.用户新增订票信息的接口
//POST /ticket_add
// 请求主体 tname=普陀山风景区&type=成人票&time=2022-7-11&number=1&price=160
//测试网址：http://127.0.0.1:8080/v1/user/ticket_add
router.post('/ticket_add',(req,res)=>{
	let name = req.body.tname
	let type = req.body.type
	let time = req.body.time
	let number = req.body.number
	let price = req.body.price
	let sql = 'insert into ticket(tname,type,time,number,price) values(?,?,?,?,?)'
	pool.query(sql,[name,type,time,time,number,price],(err,result)=>{
		if(err) throw err
		console.log(result)
		res.send({code:200,msg:'添加成功！'})
	})
})

// 11.添加游客身份信息的接口
//POST /visitor_add
//请求主体 visitor_name=于甜&identity_card=450104197710101511&phone=17640029876
//测试地址：http://127.0.0.1:8080/v1/user/visitor_add
router.post('/visitor_add',(req,res,next)=>{
	let n = req.body.visitor_name
	let i = req.body.identity_card
	let p = req.body.phone
	if(!n){
		res.send({code:201,msg:'姓名不能为空！'})
		return
	}
	if(!p){
		res.send({code:202,msg:'手机号不能为空！'})
		return
	}
	if(!/^\d{15}|\d{18}$/.test(i)){
		res.send({code:203,msg:'身份证号格式不正确！'})
		return
	}
	if(!/^(13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\d{8}$/.test(p)){
			res.send({code:204,msg:'电话格式错误！'})
			return
		}
	let sql = 'insert into user(visitor_name,identity_card,phone) values(?,?,?)'
	pool.query(sql,[n,i,p],(err,result)=>{
		if(err){
			return next(err)
		}
		res.send({code:200,msg:'添加成功！'})
	})
})